import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
import path from 'path'

export default defineConfig({
  resolve: {
    alias: {
      '@': path.resolve(__dirname, 'src'),
      // 可以添加更多别名
      '@components': path.resolve(__dirname, 'src/components'),
      '@views': path.resolve(__dirname, 'src/views'),
      '@assets': path.resolve(__dirname, 'src/assets')
    }
  },

  plugins: [vue()],
  server: {
    cors: {
      origin: '*',
      methods: ['GET', 'POST', 'PUT', 'DELETE', 'OPTIONS'],
      allowedHeaders: ['Content-Type', 'Authorization'],
      credentials: true
    },

    proxy: {
      '/front': {
        target: 'https://localhost:8080/front',
        changeOrigin: true,
        rewrite: (path) => path.replace(/^\/front/, '')
      },

      '/video': {
        target: 'https://sample-videos.com',
        changeOrigin: true,
        rewrite: (path) => path.replace(/^\/video/, '')
      },
      '/api': {
        target: 'http://60.255.146.185:20000',
        changeOrigin: true,
        rewrite: (path) => path.replace(/^\/api/, ''),
        configure: (proxy, options) => {
          proxy.on('proxyReq', (proxyReq, req, res) => {
            if (req.headers.authorization) {
              proxyReq.setHeader('Authorization', req.headers.authorization)
            }
          })
        }
      }
    }
  },
  publicDir: 'public'
})
